package com.ithiema.edu.info.manager.dao;

import com.ithiema.edu.info.manager.domain.Teacher;
import com.ithiema.edu.info.manager.utils.Utils;

public class TeacherDao {
    private static Teacher[] teas = new Teacher[5];
    Utils utils = new Utils();

    private static int count = 0;//计数器

    public boolean addTeacher(Teacher teacher) {
        for (int i = 0; i < teas.length; i++) {
            if (teas[i] == null) {
                teas[i] = teacher;
                count++;//添加教师，计数器加一
                return true;
            }
        }
        return false;
    }

    public Teacher[] findAllTeacher() {
        return teas;
    }

    public int findCount() {
        return count;
    }

    public boolean deleteTeacehr(String id) {
        int index = utils.InquireIndex(id, teas);
        teas[index] = null;
        count--;//删除教师，计数器减一
        arrangeTeachers();//删除后，整理一次数组
        return true;
    }

    //整理数组
    private void arrangeTeachers() {
        for (int i = 0; i < count; i++) {
            Teacher tea = teas[i];
            if (tea == null) {
                for (int j = i + 1; j < teas.length; j++) {
                    Teacher tea_j = teas[j];
                    if (tea_j != null) {
                        teas[i] = tea_j;
                        teas[j] = null;
                    }
                }
            }
        }
    }

    public void reviseTeacehr(String id, Teacher teacher) {
        int index = utils.InquireIndex(id, teas);
        teas[index] = teacher;
    }
}
